package com.service.impl;

import com.dao.FriendDao;
import com.dao.FriendMessageDao;
import com.dao.PostingInfoDao;
import com.dao.PostingVisitPrivilegeDao;
import com.entity.Friend;
import com.entity.PostingInfo;
import com.entity.PostingVisitPrivilege;
import com.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {
    //好友信息
    @Autowired
    private FriendDao friendDao;
    //好友聊天信息
    @Autowired
    public FriendMessageDao friendMessageDao;
    //帖子信息
    @Autowired
    public PostingInfoDao postingInfoDao;
    //帖子权限
    @Autowired
    public PostingVisitPrivilegeDao postingVisitPrivilegeDao;

    @Override
    public List<Friend> getAll() {
        return friendDao.getAll();
    }

    @Override
    public List<Friend> getByTypeId(Integer id) {
        return friendDao.getByTypeId(id);
    }
    //修改
    @Override
    public Boolean editFriend(Friend friend) {
        return friendDao.editFriend(friend);
    }
    //删除
    @Override
    @Transactional
    public Boolean delFriend(Integer userId, Integer friendId) {
        //获取当前全部的帖子ID
        List<Integer> userPostingIds = postingInfoDao.getUserPostingInfoAllId(userId);
        //获取好友的的全部帖子ID
        List<Integer> friendPostingIds = postingInfoDao.getUserPostingInfoAllId(friendId);
        //创建权限
        PostingVisitPrivilege postingVisitPrivilege = new PostingVisitPrivilege();
        
        //删除当前用户帖子可以访问的帖子权限
        for (Integer userPostingId : userPostingIds) {
            postingVisitPrivilege.setUserId(friendId);
            postingVisitPrivilege.setPostingId(userPostingId);
            postingVisitPrivilegeDao.delPostingVisitPrivilege(postingVisitPrivilege);
        }
        //删除好友的帖子可以访问权限
        for (Integer friendPostingId : friendPostingIds) {
            postingVisitPrivilege.setUserId(userId);
            postingVisitPrivilege.setPostingId(friendPostingId);
            postingVisitPrivilegeDao.delPostingVisitPrivilege(postingVisitPrivilege);
        }
        
        //删除聊天信息
        friendMessageDao.delFriendMessage(userId, friendId);
        
        return friendDao.delFriend(userId, friendId);
    }
}
